package com.wxy.magic;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by WXY on 2018/2/10.
 */

public class ADactivity extends AppCompatActivity {

    private static final String TAG = ADactivity.class.getSimpleName();
    TextView text_time;
    private Disposable mDisposable;
    int count = 3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ad);

        text_time = findViewById(R.id.text_time);
        showAD();
    }

    private void showAD() {
        mDisposable = Flowable.interval(1, TimeUnit.SECONDS)
                .doOnNext(aLong -> Log.e(TAG, "accept: doOnNext : " + aLong))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                            Log.e(TAG, "accept: 设置文本 ：" + aLong);
                            if (aLong > count) {
                                mDisposable.dispose();
                                startActivity(new Intent(this, MainActivity.class));
                                finish();
                            } else {
                                text_time.setText("倒计时：" + (count - aLong) + "秒");
                            }
                        }
                );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }
}
